package com.cloudmusic.api.controller;

import com.cloudmusic.api.entity.Banner;
import com.cloudmusic.api.service.BannerService;
import com.cloudmusic.api.utils.QiniuyunUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author 王金
 * @create 2020-09-27
 * @since 1.0.0
 */
@Controller
@RequestMapping("banner")
public class BannerController {

    private final BannerService bannerService;

    @Autowired
    public BannerController(BannerService bannerService) {
        this.bannerService = bannerService;
    }

    @Autowired
    QiniuyunUtil qiniuyunUtil;


    @ResponseBody
    @RequestMapping("add")
    public int addBanner(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        String pictureUrl = qiniuyunUtil.uploadPicture(multipartFile);
        Banner banner = new Banner();
        banner.setBannerPath(pictureUrl);
        banner.setBannerNo((int) new Date().getTime());
        return bannerService.addBanner(banner);
    }

    @ResponseBody
    @RequestMapping("del")
    public int delBannerByPrimaryKey(Integer bannerId) {
        return bannerService.modifyBannerStatus(bannerId);
    }

    @ResponseBody
    @RequestMapping("update")
    public int updateBanner(@RequestParam("file") MultipartFile multipartFile,
                            @RequestParam("bannerId") Integer bannerId) throws IOException {
        Banner banner = bannerService.findSingleBannerByPrimaryKey(bannerId);
        banner.setBannerPath(qiniuyunUtil.uploadPicture(multipartFile));
        return bannerService.modifyBanner(banner);
    }

    @ResponseBody
    @RequestMapping("showSingleBanner")
    public Banner showSingleBanner(Integer bannerId) {
        return bannerService.findSingleBannerByPrimaryKey(bannerId);
    }

    @ResponseBody
    @RequestMapping("listBanner")
    public List<Banner> listBanner() {
        return bannerService.findAllBanner();
    }


}
